import {
    apiBase
} from './common';
export function login() {
    return new Promise((resolve, reject) => {
        wx.login({
            success: function (options) {
                const jsCode = options.code;
                wx.request({
                    url: `${apiBase}wx/login`,
                    data: {
                        code: jsCode
                    },
                    header: {
                        'content-type': 'application/x-www-form-urlencoded; charset=utf-8'
                    },
                    dataType: 'json',
                    method: 'POST',
                    success: (data, statusCode, header) => {
                        resolve(data.data);
                    },
                    fail: () => {
                        console.log(arguments);
                        reject(arguments);
                    }
                });
            },
            fail: function () {
                console.log(arguments);
                reject(arguments);
            }
        });
    });
}

export function getUserInfo() {
    return checkSession().then(() => {
        return new Promise((resolve, reject) => {
            wx.getSetting({
                success: function (res) {
                    console.log(res);
                    if (res.authSetting['scope.userInfo']) {
                        wx.getUserInfo({
                            withCredentials: true,
                            success: function (res) {
                                console.log(res);
                                resolve(res);
                            },
                            fail: function(){
                                console.log(arguments);
                                reject(arguments);
                            }
                        })
                    }else{
                        resolve(null);
                    }
                },
                fail: function(){
                    console.log(arguments);
                    reject(arguments);
                }
            })
        });
    })
}

export function getFAIUserInfo(faiSessionKey){
    return new Promise((resolve, reject) => {
        wx.request({
            url: `${apiBase}user`,
            header:{
                'content-type': 'application/x-www-form-urlencoded; charset=utf-8' 
            },
            data:{
                'faiSessionKey': faiSessionKey,
            },
            dataType:'json',
            method: 'GET',
            success: (data, statusCode, header) => {
                resolve(data.data);
            },
            fail:  ()=>{
                console.log(arguments);
                reject(arguments);
            }
        });
    });
}

function checkSession() {
    return new Promise((resolve, reject) => {
        wx.checkSession({
            success: function () {
                resolve();
            },
            fail: function () {
                login().then(() => {
                    resolve();
                }).catch(reason => {
                    reject(reason);
                })
            }
        })
    })
}